﻿using Abp.Domain.Repositories;
using LZ.DCode.DataDictionarys.Dtos;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LZ.DCode.DataDictionarys.DomainService
{
    /// <summary>
    /// 领域服务层一个模块的核心业务逻辑
    ///</summary>
    public class DataGroupManager : DCodeDomainServiceBase, IDataGroupManager
    {

        private readonly IRepository<DataGroup> _dataGroupRepository;

        /// <summary>
        /// DataGroup的构造方法
        /// 通过构造函数注册服务到依赖注入容器中
        ///</summary>
        public DataGroupManager(IRepository<DataGroup> dataGroupRepository)
        {
            _dataGroupRepository = dataGroupRepository;
        }

        public async Task<List<DataGroup>> GetGroupsAsync(string groupType)
        {
            var query = _dataGroupRepository.GetAll().Where(d => d.IsShow && d.Type == groupType);

            var result = await query.OrderBy(q => q.Seq).ToListAsync();

            return result;
        }

        public async Task<List<GroupType>> GetGroupTypeAsync()
        {
            var query = _dataGroupRepository.GetAll().Where(d => d.IsShow).Select(d => d.Type).Distinct();

            var result = await query.OrderBy(q => q).ToListAsync();

            var types = new List<GroupType>();
            //如果发现有.,配置约定排序, 如：01.系统配置 02.设备管理
            if (result.Any(r => r.IndexOf('.') != -1))
            {
                result.ForEach((r) =>
                {
                    var type = new GroupType();
                    type.Name = r;
                    type.ShowName = r.Substring(r.IndexOf('.') + 1);//截取前面

                    types.Add(type);
                });
            }
            else
            {
                result.ForEach((r) =>
                {
                    var type = new GroupType();
                    type.Name = r;
                    type.ShowName = r;//截取前面

                    types.Add(type);
                });
            }

            return types;
        }
    }
}

